package com.heifeng.cloudshelf.mvp.product.presenter;

import com.heifeng.cloudshelf.base.BaseResponse;
import com.heifeng.cloudshelf.base.RxPresenter;
import com.heifeng.cloudshelf.dao.ProductBean;
import com.heifeng.cloudshelf.helper.MyObserver;
import com.heifeng.cloudshelf.helper.RxUtil;
import com.heifeng.cloudshelf.mvp.product.contract.ProductActivityContract;
import com.heifeng.cloudshelf.network.NetClient;
import com.heifeng.cloudshelf.utils.LoginErrorUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2019/9/15.
 */

public class ProductActivityPresenter extends RxPresenter<ProductActivityContract.View> implements ProductActivityContract.Presenter {

    private Map<String,Integer> templateIds = new HashMap<>();

    @Inject
    public ProductActivityPresenter() {
    }

    @Override
    public void init() {

    }

    @Override
    public void toGetAllProduct() {
        NetClient.getInstance().getNetApi().toGetAllProduct()
                .compose(RxUtil.<BaseResponse<ProductBean>>rxSchedulers())
                .subscribe(new MyObserver<BaseResponse<ProductBean>>(mContext) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<ProductBean> baseResponse) {
                        if (0 == baseResponse.getCode()) {
                            ProductBean getresult = baseResponse.getresult();
                            List<ProductBean.PlansContentsBean> plansContents = getresult.getPlansContents();
                            if (null != plansContents && plansContents.size() > 0) {
                                for (int i = 0; i < plansContents.size(); i++) {
                                    templateIds.put(plansContents.get(i).getName(),plansContents.get(i).getTemplateId());
                                }
                            }
                            if (null != mView) {
                                mView.setWeiXinImage(getresult.getMiniQrUrl());
                                mView.startView();
                            }
                        }else{
                            LoginErrorUtils.loginOut(mContext,baseResponse.getCode(),baseResponse.getmsg());
                        }
                    }
                });
    }

    @Override
    public int getTemplateId(String name) {
        if (null != templateIds && templateIds.size() > 0 && templateIds.containsKey(name)) {
            return templateIds.get(name);
        }
        return 0;
    }

    @Override
    public boolean isInitTemplateId() {
        return null != templateIds && templateIds.size() > 0;
    }


}
